#pragma once

#include <vector>
#include <string>

#include "IFinsCommand.h"
#include "net.h"

using namespace std;

namespace OmronPlc {
	class udpFinsCommand : public IFinsCommand {
	private:
		fins_command_t *finsCommand;
		fins_command_t *finsResponse;

		int finsResponseLen = 0;
		byte sendFinsData[2048];
		byte respFinsData[2048];

		UdpClient* transport;
		EndPoint* remoteEP;
		int connectStst;

	public:
		udpFinsCommand(uint8_t ServiceID = 0x02);
		virtual ~udpFinsCommand();
		virtual bool Connect();
		virtual bool IsConnectd();
		virtual void Close();
		virtual void SetRemote(cstr_t ipaddr, uint16_t port);
		virtual bool MemoryAreaRead(MemoryArea area, uint16_t address, uint8_t bit_position, uint16_t count);
		virtual bool MemoryAreaWrite(MemoryArea area, uint16_t address, uint8_t bit_position, uint16_t count, uint8_t data[]);

	private:
		bool FrameSend(byte* data = NULL, int wsize = 0);
		int Send(const uint8_t command[], int cmdLen);
		int Receive(uint8_t response[], int respLen);
	};
}
